package com.xxgc.moriserver.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxgc.moriserver.api.WechatLoginApi;
import com.xxgc.moriserver.api.response.WechatLoginApiResponse;
import com.xxgc.moriserver.controller.dto.UserLoginDto;
import com.xxgc.moriserver.entity.UserEntity;
import com.xxgc.moriserver.entity.UserEntityRepository;
import com.xxgc.moriserver.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/login")
@RestController
@Tag(name = "登录接口")
public class UserController {
    private static final String appId = "wx64a414809c0b9938";
    private static final String appSecret = "ac204c80b8d9bb4feb6bcc5fcc604943";

    @Autowired
    private WechatLoginApi wechatLoginApi;

    @Autowired
    private UserEntityRepository userEntityRepository;

    @Operation(summary = "微信登录")
    @PostMapping("/wechatLogin/{code}")
    public String wechatLogin(@PathVariable String code) {
        System.out.println("code:" + code);
        WechatLoginApiResponse authorization_code = wechatLoginApi.getUserOpenId(appId, appSecret, code, "authorization_code");
        System.out.println("authorization_code = " + authorization_code);

        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.eq("open_id", authorization_code.getOpenid());
        UserEntity one = userEntityRepository.getOne(qw);
        if (one == null) {
            one = new UserEntity();
            one.setOpenId(authorization_code.getOpenid());
            userEntityRepository.save(one);
        }
        return one.getUserId();
    }

    @Operation(summary = "游戏登录")
    @PostMapping("/gameLogin")
    public Result GameLogin(@RequestBody Map map) {
//        返回一个String类型的用户ID
        System.out.println("map = " + map);

        String nickname = (String)map.get("nickname");
        String password = (String)map.get("password");

        QueryWrapper<UserEntity> qw= new QueryWrapper<>();
        qw.eq("nick_name",nickname);
        UserEntity a =userEntityRepository.getOne(qw);
        if(a == null){
            //插入数据
            UserEntity usersEntity = new UserEntity();
            usersEntity.setNickName(nickname);
            usersEntity.setUserPass(password);
            usersEntity.setLevel(1);
            userEntityRepository.save(usersEntity);
            return Result.success().msg("注册成功").code(11012);
        }else{
            //登录逻辑
            qw.eq("user_pass",password);
            UserEntity b = userEntityRepository.getOne(qw);
            if(b == null){
                return Result.fail("用户名或密码错误");
            }else{
                    String id =b.getUserId();

                    StpUtil.login(id);

                    String tokenValue=StpUtil.getTokenValue();

                UserLoginDto userLoginDTO = new UserLoginDto();
                userLoginDTO.setToken(tokenValue);
                userLoginDTO.setUserId(id);
                userLoginDTO.setNickName(b.getNickName());
                userLoginDTO.setAvatar(b.getAvatar());
                userLoginDTO.setGold(b.getGold());
                userLoginDTO.setLevel(b.getLevel());
                userLoginDTO.setSex(b.getSex());

                return Result.success(userLoginDTO).msg("欢迎使用");
            }
        }

    }

    @GetMapping("/getMsg")
    public Result getMsg(){
        return Result.success("msg");
    }
    @GetMapping("/getInfo")
    public Result getInfo(){
        return Result.success("info");
    }
}
